package com.lzx.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.answer.entity.Problem;
import com.lzx.answer.entity.Reply;
import com.lzx.answer.entity.ReplyGiveALike;
import com.lzx.answer.entity.vo.User;
import com.lzx.answer.feign.UserClient;
import com.lzx.answer.mapper.ProblemMapper;
import com.lzx.answer.mapper.ReplyGiveALikeMapper;
import com.lzx.answer.mapper.ReplyMapper;
import com.lzx.answer.service.ReplyGiveALikeService;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-10-13
 */
@Service
public class ReplyGiveALikeServiceImpl extends ServiceImpl<ReplyGiveALikeMapper, ReplyGiveALike> implements ReplyGiveALikeService {
    @Resource
    private ReplyGiveALikeMapper replyGiveALikeMapper;

    @Resource
    private ReplyMapper replyMapper;

    @Resource
    private UserClient userClient;

    @Resource
    private ProblemMapper problemMapper;

    //问题回复点赞
    @GlobalTransactional
    @Override
    public R likeReply(ReplyGiveALike replyGiveALike) {
        QueryWrapper<ReplyGiveALike> wrapper = new QueryWrapper<>();
        wrapper.eq("problem_id", replyGiveALike.getProblemId()).eq("parent_id", replyGiveALike.getParentId()).eq("user_id", replyGiveALike.getUserId());
        ReplyGiveALike vo = replyGiveALikeMapper.selectOne(wrapper);
        if (vo != null) {
            if (vo.getIsLike() == 1) {
                vo.setIsLike(0);
                vo.setCreateTime(new Date());
                //更新用户点赞信息
                replyGiveALikeMapper.updateById(vo);
                //回复的这条记录点赞人数-1
                Reply reply = replyMapper.selectById(replyGiveALike.getParentId());
                reply.setLikeCount(reply.getLikeCount() - 1);
                replyMapper.updateById(reply);
                return R.ok().message("取消点赞");
            } else {
                vo.setIsLike(1);
                vo.setCreateTime(new Date());
                //更新用户点赞信息
                replyGiveALikeMapper.updateById(vo);
                //回复的这条记录点赞人数+1
                Reply reply = replyMapper.selectById(replyGiveALike.getParentId());
                reply.setLikeCount(reply.getLikeCount() + 1);
                replyMapper.updateById(reply);
                return R.ok().message("点赞成功");
            }
        } else {
            //保存用户点赞信息
            replyGiveALike.setIsLike(1);
            replyGiveALike.setCreateTime(new Date());
            replyGiveALikeMapper.insert(replyGiveALike);
            //回复的这条记录点赞人数+1
            Reply reply = replyMapper.selectById(replyGiveALike.getParentId());
            reply.setLikeCount(reply.getLikeCount() + 1);
            replyMapper.updateById(reply);

            //回复的用户积分+1
            userClient.likeProblem(Math.toIntExact(reply.getUserId()));

            //发送通知
            Inform inform = new Inform();
            inform.setUserId(Math.toIntExact(reply.getUserId()));
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(replyGiveALike.getUserId()));
            inform.setSender(replyGiveALike.getUserId());
            inform.setNickname(user.getNickname());
            inform.setAvatar(user.getAvatar());
            inform.setType(InformType.LIKE.getType());
            Problem problem = problemMapper.selectById(replyGiveALike.getProblemId());
            inform.setMessage(problem.getTitle());
            userClient.saveInform(inform);

            return R.ok().message("点赞成功");
        }
    }

    //根据问题id和用户id查询用户点赞信息
    @Override
    public R selectReplyGiveALive(Long problemId, Long userId) {
        QueryWrapper<ReplyGiveALike> wrapper = new QueryWrapper<>();
        wrapper.eq("problem_id", problemId).eq("user_id", userId);
        List<ReplyGiveALike> list = replyGiveALikeMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }
}
